package com.hs.srm.admin.modules.pricemanagement.service.impl;

import java.util.List;
import java.util.Map;

import com.hs.srm.base.common.utils.DictDataUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.pricemanagement.dao.ParityDao;
import com.hs.srm.admin.modules.pricemanagement.entity.InquiryQuotationEntity;
import com.hs.srm.admin.modules.pricemanagement.service.ParityService;
import com.hs.srm.admin.modules.pricemanagement.vo.InquiryVO;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.Constant;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;

/**
 * @author yanfei
 * @date 2019年3月1日
 * 询价单头信息
 */
@Service("parityService")
public class ParityServiceImpl extends ServiceImpl<ParityDao, InquiryQuotationEntity> implements ParityService {

    /**
     * 分页查询
     */
    @Override
    @DataFilter
    public PageUtils queryPage(Map<String, Object> params) {
        Page<InquiryQuotationEntity> page = this.selectPage(
                new QueryParam<InquiryQuotationEntity>(params).getPage(),
                new EntityWrapper<InquiryQuotationEntity>()
                        .addFilterIfNeed(!ObjectUtils.isEmpty(params.get(Constant.SQL_FILTER)),
                                (String) params.get(Constant.SQL_FILTER)));
        return new PageUtils(page);
    }

    @Override
    public PageUtils selectDetailPage(Map<String, Object> params) {
        Page<InquiryQuotationEntity> page = this.selectDetailPage(
                new QueryParam<InquiryQuotationEntity>(params).getPage(), new EntityWrapper<InquiryQuotationEntity>(),
                params);
        //单位valToTxt
        List<InquiryQuotationEntity> list = page.getRecords();
        list.forEach(quotationEntity -> quotationEntity.setPoPriceUnit(DictDataUtil.valToTxt(quotationEntity.getPoPriceUnit(), DictDataUtil.DictType.JLDW)));
        return new PageUtils(page);
    }

    private Page<InquiryQuotationEntity> selectDetailPage(Page<InquiryQuotationEntity> page,
                                                          Wrapper<InquiryQuotationEntity> wrapper, Map<String, Object> params) {
        page.setRecords(this.baseMapper.selectDetailPage(page, wrapper, params));
        return page;
    }

    @Override
    public InquiryVO findbyId(String id) {
        return this.baseMapper.findbyId(id);
    }

    // 报价单添加询价金额
    @Override
    public boolean updateEnquiryPrice(InquiryQuotationEntity quotationEntity) {
        return this.baseMapper.updateEnquiryPrice(quotationEntity);
    }

    @Override
    public boolean updateTab(InquiryQuotationEntity quotationEntity) {
        return this.baseMapper.updateTab(quotationEntity);
    }

    public void deleteParity(String quotationId) {
        InquiryQuotationEntity quotationEntity = this.baseMapper.selectById(quotationId);
        quotationEntity.setDeleteFlag(1);
        this.baseMapper.updateById(quotationEntity);
    }

    /*
     * 更新全部字段方法
     * */
    public void updateAll(InquiryQuotationEntity inquiryQuotationEntity) {
        this.baseMapper.updateById(inquiryQuotationEntity);
    }
}
